package com.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.config.exceptionHandler.exception.GuliException;
import com.edu.entity.Teacher;
import com.edu.entity.vo.TeacherQuery;
import com.edu.service.TeacherService;
import com.edu.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 * 统一采用restful风格
 *
 * @author mxs
 * @since 2021-10-13
 */
@RestController
@RequestMapping("/edu/teacher")
//必须要加上@crossOrigin 解决跨域问题
@CrossOrigin
@Api(tags = "教师管理模块")//描述这个接口
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 找到所有的教师Teacher
     *
     * @return
     */
    @ApiOperation(value = "查找所有教师")//描述这个方法
    @GetMapping("findAll")
    public R findAll() {
        List<Teacher> list = teacherService.list(null);
        return R.success().data("items", list);
    }

    /**
     * 根据id删除教师（逻辑删除，将deleted项设为1）
     *
     * @param id 教师id
     * @return 是否删除成功
     */
    @ApiOperation(value = "逻辑删除老师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name = "id", value = "老师id", required = true) //参数的描述
                           @PathVariable Long id) {
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.success().data("isdelete", true).message("删除成功");
        }
        return R.error().data("isdeleted", false).message("删除失败");

    }

    /**
     * 分页查询教师
     *
     * @param pageNo 查询页码
     * @param limit  每页限制数量
     * @return 返回记录数 和 当期页的数据
     */
    @GetMapping("/pageListTeacher/{pageNo}/{limit}")
    public R pageListTeacher(@PathVariable("pageNo") long pageNo,
                             @PathVariable("limit") long limit) {
        Page<Teacher> page = new Page<>(pageNo, limit);
        //根据创建时间倒序排序
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        teacherService.page(page, queryWrapper);
        long total = page.getTotal();
        List<Teacher> records = page.getRecords();
        return R.success().data("total", total).data("records", records);
    }
    /**
     * 动态查询教师
     *
     * @param pageNo       查询页码
     * @param limit        限制每页数量条数
     * @param teacherQuery 条件查询对象
     * @return 需要注意，如果参数TeacherQuery没有加@RequestBody，那么请求方式可以是get
     * 如果加了注解就必须要用post请求的方式
     */
    @PostMapping("/pageListTeacherCondition/{pageNo}/{limit}")
    public R pageTeacherCondition(@PathVariable("pageNo") long pageNo,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<Teacher> teacherPage = new Page<>(pageNo, limit);
        //创建条件查询wrapper对象
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        //下面是为了动态查询，后面会封装到service中
        String id = teacherQuery.getId();
        String name = teacherQuery.getName();
        Date gmtCreate = teacherQuery.getGmtCreate();
        Date gmtModified = teacherQuery.getGmtModified();
        Integer level = teacherQuery.getLevel();
        if (!StringUtils.isEmpty(id)) {
            queryWrapper.eq("id", id);
        }
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(gmtCreate)) {
            queryWrapper.gt("gmt_create", gmtCreate);
        }
        if (!StringUtils.isEmpty(gmtModified)) {
            queryWrapper.lt("gmt_modified", gmtModified);
        }
        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }
        //条件查询
        teacherService.page(teacherPage, queryWrapper);
        //获取数据
        List<Teacher> records = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        return R.success().data("records", records).data("total", total);
    }

    /**
     * 添加 增加 add insert
     * 插入新的教师新数据
     *
     * @param teacher
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if (save) {
            return R.success().data("save", save);
        } else {
            return R.error().data("sace", save);
        }
    }

    /**
     * 根据id获取教师
     *
     * @param id 教师id
     * @return
     */
    @GetMapping("{id}")
    public R getTeacher(@PathVariable("id") long id) {
        Teacher teacher = teacherService.getById(id);
        return R.success().data("teacher", teacher);
    }

    /**
     * 根据教师id更新教师数据
     *
     * @param teacher 更新的教师
     * @return
     */
    @PostMapping("/updateTeacher")
    public R updateTeacherById(@RequestBody Teacher teacher) {
        boolean b = teacherService.updateById(teacher);
        if (b) {
            return R.success().data("change:", b);
        } else {
            return R.error().data("change:", b);
        }
    }
}


















